package com.fancon.android.gardenjokesx.adapter;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fancon.android.emoji.widget.RichTextView;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.DetailJokesActivity;
import com.fancon.android.gardenjokesx.activity.M042Activity;
import com.fancon.android.gardenjokesx.activity.M05Activity;
import com.fancon.android.gardenjokesx.activity.ProfileActivity;
import com.fancon.android.gardenjokesx.entity.UserNews;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.StringUtil;
import com.fancon.android.gardenjokesx.utils.TimeUtil;
import com.fancon.android.ui.widget.ImageCacheView;

public class UserNewAdapter extends BaseAdapter {

	// UserNews userNew;
	private Activity mContext;
	private ArrayList<UserNews> mListUser;
	private LayoutInflater inflater;
	static int number = 0;

	public UserNewAdapter(Activity context, ArrayList<UserNews> listUser) {
		mContext = context;
		mListUser = listUser;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		number = 0;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (mListUser.size() <= 100) {
			return mListUser.size();
		} else {
			return 100;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// Log.d("position", ""+position);
		ViewHolder holder = null;
		View vi = convertView;
		try {
			final UserNews userNew = mListUser.get(position);
			final int type = userNew.getType();
			if (vi == null) {
				vi = inflater.inflate(R.layout.u21_common_item, null);
				holder = new ViewHolder();
				holder.imgAvatar = (ImageCacheView) vi.findViewById(R.id.U21_common_imgAvar);
				holder.imgPennants = (ImageCacheView) vi.findViewById(R.id.U21_common_imgPennants);
				holder.txtMessage = (TextView) vi.findViewById(R.id.U21_common_txtMSG);
				holder.imgThumbnail = (ImageCacheView) vi.findViewById(R.id.U21_common_imgThumbnail);
				holder.txtTimePost = (TextView) vi.findViewById(R.id.U21_common_txtTime);
				holder.btnReply = (Button) vi.findViewById(R.id.U21_common_btnReply);
				holder.txtComment = (RichTextView) vi.findViewById(R.id.U21_common_comment);
				holder.layoutComment = (RelativeLayout) vi.findViewById(R.id.U21_layout_comment);
				vi.setTag(holder);
			} else {
				holder = (ViewHolder) vi.getTag();
			}

			holder.txtMessage.setText("");
			holder.txtComment.setVisibility(View.GONE);
			holder.btnReply.setVisibility(View.GONE);
			holder.layoutComment.setVisibility(View.GONE);
			String text = "";
			Spannable span;
			switch (type) {

			// photo of user be clicked button vote
			case 1:
				String textClick = "";
				holder.imgAvatar.setVisibility(View.VISIBLE);
				holder.imgPennants.setVisibility(View.GONE);
				holder.imgThumbnail.setVisibility(View.VISIBLE);
				holder.imgAvatar.loadImage(userNew.getUrl_avatar());
				holder.imgThumbnail.loadImage(userNew.getUrl_image());
				String textSaid = "";
				switch (Integer.valueOf(userNew.getContent())) {
				case Constant.BUTTON_TYPE_LIKE:
					textSaid = mContext.getString(R.string.Common_Like);
					break;
				case Constant.BUTTON_TYPE_CUTE:
					textSaid = mContext.getString(R.string.Common_Cute);
					break;
				case Constant.BUTTON_TYPE_COOL:
					textSaid = mContext.getString(R.string.Common_Cool);
					break;
				case Constant.BUTTON_TYPE_YUMMY:
					textSaid = mContext.getString(R.string.Common_Yummy);
					break;
				default:
					break;
				}
				ArrayList<UserNews> tmpUser = new ArrayList<UserNews>();
				tmpUser = mListUser.get(position).getNews();
				if (tmpUser.size() < 3 && tmpUser.size() >= 0) {
					String text2 = "";
					if (tmpUser.size() > 0) {
						for (int i = 0; i < tmpUser.size(); i++) {
							if (i == tmpUser.size() - 1) {
								text2 = text2 + tmpUser.get(i).getUser_name();
							} else {
								text2 = text2 + tmpUser.get(i).getUser_name() + ",";
							}
						}
						text = mContext.getString(R.string.u21_MSG_U0020, mListUser.get(position).getUser_name() + ","
								+ text2, textSaid);
					} else {
						text = mContext.getString(R.string.u21_MSG_U0020, mListUser.get(position).getUser_name(),
								textSaid);
					}
				} else {
					//String language = Locale.getDefault().getDisplayLanguage();
					if (tmpUser.size() == 3) {
						text = mContext.getString(R.string.u21_MSG_U0030, mListUser.get(position).getUser_name(),
								tmpUser.get(0).getUser_name(), tmpUser.get(1).getUser_name(),
								String.valueOf(tmpUser.size() - 2), textSaid);
							text = text.replace("1 others", "another");
							textClick = "another";
					}
					if (tmpUser.size() > 3) {
						text = mContext.getString(R.string.u21_MSG_U0030, mListUser.get(position).getUser_name(),
								tmpUser.get(0).getUser_name(), tmpUser.get(1).getUser_name(),
								String.valueOf(tmpUser.size() - 2), textSaid);
							textClick = String.valueOf(tmpUser.size() - 2) + " " + "others";
					}
				}
				Log.d("U21 message", text);
				span = new SpannableString(text);
				for (int i = 0; i < tmpUser.size(); i++) {
					int index = text.indexOf(tmpUser.get(i).getUser_name());
					final String id = tmpUser.get(i).getUser_id();
					if (index > 0) {
						span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
							@Override
							public void onClick(View widget) {
								Intent in = new Intent(mContext, ProfileActivity.class);
								in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								in.putExtra(Constant.USER_ID_VIEW, id);
								mContext.startActivity(in);
							}
						}, index, index + tmpUser.get(i).getUser_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					}
				}
				int indexClick = text.indexOf(textClick);
				if (indexClick > 0) {
					span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
						@Override
						public void onClick(View widget) {
							Intent in = new Intent(mContext, M042Activity.class);
							in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							in.putExtra(Constant.PHOTO_ID, mListUser.get(position).getObject_id());
							mContext.startActivity(in);
						}
					}, indexClick, indexClick + textClick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				}
				span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
					@Override
					public void onClick(View widget) {
						Intent i = new Intent(mContext, ProfileActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						i.putExtra(Constant.USER_ID_VIEW, mListUser.get(position).getUser_id());
						mContext.startActivity(i);
					}
				}, 0, mListUser.get(position).getUser_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				holder.txtMessage.setText(span);
				// mTempUser.clear();
				break;
			//
//			case 21:
//			case 22:
//				String language = Locale.getDefault().getDisplayLanguage();
//				holder.imgAvatar.setVisibility(View.GONE);
//				holder.imgPennants.setVisibility(View.GONE);
//				holder.imgThumbnail.setVisibility(View.VISIBLE);
//				holder.imgThumbnail.loadImage(userNew.getUrl_image());
//				if (language.equals("日本語")) {
//					// User get Vanguard
//					if (type == 21) {
//						span = new SpannableString("#" + userNew.getContent());
//						span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
//
//							@Override
//							public void onClick(View widget) {
//								Intent i = new Intent(mContext, T02Activity.class);
//								i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//								i.putExtra(Constant.TAG_NAME, userNew.getContent());
//								mContext.startActivity(i);
//							}
//						}, 0, userNew.getContent().toString().length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//						holder.txtMessage.append("あなたは");
//						holder.txtMessage.append(span);
//						holder.txtMessage.append(" の");
//						holder.txtMessage.append(Html.fromHtml("<img src=\"u21_vanga_icon.png\"/>", imgGetterVangua,
//								null));
//						holder.txtMessage.append("angaurdになりました。");
//					}
//					// user get Master
//					if (type == 22) {
//						span = new SpannableString("#" + userNew.getContent());
//						span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
//
//							@Override
//							public void onClick(View widget) {
//								Intent i = new Intent(mContext, T02Activity.class);
//								i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//								i.putExtra(Constant.TAG_NAME, userNew.getContent());
//								mContext.startActivity(i);
//							}
//						}, 0, userNew.getContent().toString().length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//						holder.txtMessage.append("あなたは");
//						holder.txtMessage.append(span);
//						holder.txtMessage.append(" の");
//						holder.txtMessage.append(Html.fromHtml("<img src=\"u21_master_icon.png\"/>", imgGetterMaster,
//								null));
//						holder.txtMessage.append("asterになりました。");
//					}
//
//				} else {
//					// User get Vanguard
//					if (type == 21) {
//						text = mContext.getString(R.string.u21_MSG_U0021, "anguard", userNew.getContent());
//						int index = text.indexOf("anguard");
//						int index2 = text.indexOf(userNew.getContent());
//						String str1 = text.substring(0, index - 1);
//						String str2 = text.substring(index, index2 - 1);
//						span = new SpannableString("#" + userNew.getContent());
//						span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
//
//							@Override
//							public void onClick(View widget) {
//								Intent i = new Intent(mContext, T02Activity.class);
//								i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//								i.putExtra(Constant.TAG_NAME, userNew.getContent());
//								mContext.startActivity(i);
//							}
//						}, 0, userNew.getContent().toString().length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//						holder.txtMessage.append(str1 + " ");
//						holder.txtMessage.append(Html.fromHtml("<img src=\"u21_vanga_icon.png\"/>", imgGetterVangua,
//								null));
//						holder.txtMessage.append(str2 + " ");
//						holder.txtMessage.append(span);
//
//					}
//
//					// user get Master
//					if (type == 22) {
//						text = mContext.getString(R.string.u21_MSG_U0021, "aster", userNew.getContent());
//						int index = text.indexOf("aster");
//						int index2 = text.indexOf(userNew.getContent());
//						String str1 = text.substring(0, index - 1);
//						String str2 = text.substring(index, index2 - 1);
//						span = new SpannableString("#" + userNew.getContent());
//						span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
//
//							@Override
//							public void onClick(View widget) {
//								Intent i = new Intent(mContext, T02Activity.class);
//								i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//								i.putExtra(Constant.TAG_NAME, userNew.getContent());
//								mContext.startActivity(i);
//							}
//						}, 0, userNew.getContent().toString().length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//						holder.txtMessage.append(str1 + " ");
//						holder.txtMessage.append(Html.fromHtml("<img src=\"u21_master_icon.png\"/>", imgGetterMaster,
//								null));
//						holder.txtMessage.append(str2 + " ");
//						holder.txtMessage.append(span);
//					}
//				}
//				break;
//			case 3:
//			case 5:
//				holder.imgAvatar.setVisibility(View.GONE);
//				holder.imgPennants.setVisibility(View.GONE);
//				holder.imgThumbnail.setVisibility(View.VISIBLE);
//				holder.imgThumbnail.loadImage(userNew.getUrl_image());
//				// User lost title (Master)
//				if (type == 3) {
//					text = mContext.getString(R.string.u21_MSG_U0022, userNew.getTag_name(), userNew.getUser_name());
//					int index = text.indexOf(userNew.getUser_name());
//					span = new SpannableString(text);
//					span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
//						@Override
//						public void onClick(View widget) {
//							Intent i = new Intent(mContext, M04Activity.class);
//							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//							i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
//							mContext.startActivity(i);
//						}
//					}, index, index + userNew.getUser_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//					holder.txtMessage.setText(span);
//					holder.txtMessage.setText(mContext.getString(R.string.u21_MSG_U0022, userNew.getUser_name()));
//				}
//				// photo of user into popular
//				if (type == 5) {
//					holder.txtMessage.setText(mContext.getString(R.string.u21_MSG_U0024));
//				}
//				break;
//			// user get Pennants
//			case 4:
//				holder.imgAvatar.setVisibility(View.GONE);
//				holder.imgPennants.setVisibility(View.VISIBLE);
//				holder.imgThumbnail.setVisibility(View.GONE);
//				holder.imgPennants.loadImage(userNew.getUrl_image(), true);
//				holder.txtMessage.setText(mContext.getString(R.string.u21_MSG_U0023, userNew.getContent()));
//				break;
//
			case 6:
			case 7:
				holder.imgAvatar.setVisibility(View.VISIBLE);
				holder.imgPennants.setVisibility(View.GONE);
				holder.layoutComment.setVisibility(View.VISIBLE);
				holder.txtComment.setVisibility(View.VISIBLE);
				holder.btnReply.setVisibility(View.VISIBLE);
				holder.imgThumbnail.setVisibility(View.VISIBLE);
				holder.imgThumbnail.loadImage(userNew.getUrl_image());
				try {
					holder.txtComment.setText(URLDecoder.decode(userNew.getContent(), "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// user get new comment
				if (type == 6) {
					holder.imgAvatar.loadImage(userNew.getUrl_avatar());
					text = mContext.getString(R.string.u21_MSG_U0025, userNew.getUser_name());
					span = new SpannableString(text);
					span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
						@Override
						public void onClick(View widget) {
							Intent i = new Intent(mContext, ProfileActivity.class);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
							mContext.startActivity(i);
						}
					}, 0, userNew.getUser_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					holder.txtMessage.setText(span);
				}
				// user get comment about your self(@username)
				if (type == 7) {
					Log.d("u21", "comment ve ban than");
					holder.imgAvatar.loadImage(Temp(userNew.getUrl_avatar())[0]);
					text = mContext.getString(R.string.MSG_U0026, Temp(userNew.getUser_name())[0],
							Temp(userNew.getUser_name())[1]);
					span = new SpannableString(text);
					span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
						@Override
						public void onClick(View widget) {
							Intent i = new Intent(mContext, ProfileActivity.class);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							i.putExtra(Constant.USER_ID_VIEW, Temp(userNew.getUser_id())[0]);
							mContext.startActivity(i);
						}
					}, 0, Temp(userNew.getUser_name())[0].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					int index = text.indexOf(Temp(userNew.getUser_name())[1]);
					span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
						@Override
						public void onClick(View widget) {
							Intent i = new Intent(mContext, ProfileActivity.class);
							i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							i.putExtra(Constant.USER_ID_VIEW, Temp(userNew.getUser_id())[1]);
							mContext.startActivity(i);
						}
					}, index, index + Temp(userNew.getUser_name())[1].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					holder.txtMessage.setText(span);
				}
				break;
			case 8:
			case 9:
				holder.imgAvatar.setVisibility(View.VISIBLE);
				holder.imgPennants.setVisibility(View.GONE);
				holder.imgThumbnail.setVisibility(View.GONE);
				holder.imgAvatar.loadImage(userNew.getUrl_avatar());
				// user get follow
				if (type == 8) {
					text = mContext.getString(R.string.u21_MSG_U0027, userNew.getUser_name());
				}
				// user get request follow
				if (type == 9) {
					text = mContext.getString(R.string.u21_MSG_U0028, userNew.getUser_name());
				}
				String str1 = text.substring(0, userNew.getUser_name().length());
				String str2 = text.substring(userNew.getUser_name().length(), text.length());
				span = new SpannableString(str1);
				span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
					@Override
					public void onClick(View widget) {
						Intent i = new Intent(mContext, ProfileActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
						mContext.startActivity(i);
					}
				}, 0, str1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				holder.txtMessage.append(span);
				holder.txtMessage.append(str2);
				break;
			// friend of user on Facebook or Twitter join meshtiles
			case 10:
				holder.imgAvatar.setVisibility(View.VISIBLE);
				holder.imgPennants.setVisibility(View.GONE);
				holder.imgThumbnail.setVisibility(View.GONE);
				text = mContext.getString(R.string.u21_MSG_U0029, userNew.getUrl_image(), userNew.getUser_name());
				int index = text.indexOf(userNew.getUrl_image());
				span = new SpannableString(text);
				span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
					@Override
					public void onClick(View widget) {
						Intent i = new Intent(mContext, ProfileActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
						mContext.startActivity(i);
					}
				}, index, index + userNew.getUrl_image().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				int index2 = text.indexOf(userNew.getUser_name());
				span.setSpan(new StringUtil.NonUnderlinedClickableSpan() {
					@Override
					public void onClick(View widget) {
						Intent i = new Intent(mContext, ProfileActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
						mContext.startActivity(i);
					}
				}, index2, index2 + userNew.getUser_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				holder.txtMessage.setText(span);
				break;
			default:
				break;
			}
			holder.imgAvatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(mContext, ProfileActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					if (type != 7) {
						i.putExtra(Constant.USER_ID_VIEW, userNew.getUser_id());
					} else {
						i.putExtra(Constant.USER_ID_VIEW, Temp(userNew.getUser_id())[0]);
					}
					mContext.startActivity(i);
				}
			});
			holder.imgThumbnail.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(mContext, DetailJokesActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					i.putExtra(Constant.PHOTO_ID, userNew.getObject_id());
					i.putExtra(Constant.SCREEN_ID, Constant.PUSH_RECEIVE);
					mContext.startActivity(i);
				}
			});
			holder.btnReply.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent i = new Intent(mContext, M05Activity.class);
					i.putExtra(Constant.ID_BUTTON, Constant.BUTTON_REPLY);
					i.putExtra(Constant.PHOTO_ID, userNew.getObject_id());
					i.putExtra("viewed_user_name", Temp(userNew.getUser_name())[0]);
					i.putExtra(Constant.SCREEN_ID, Constant.U21);
					i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(i);
				}
			});
			holder.txtMessage.setLinkTextColor(mContext.getResources().getColor(R.color.common_text_isclick));
			holder.txtMessage.setMovementMethod(LinkMovementMethod.getInstance());
			holder.txtTimePost.setText(TimeUtil.getTimeCount(mListUser.get(position).getTime(), mContext));
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return vi;
	}

//	private ImageGetter imgGetterMaster = new ImageGetter() {
//
//		public Drawable getDrawable(String source) {
//			Drawable drawable = null;
//			drawable = mContext.getResources().getDrawable(R.drawable.u21_master_icon);
//			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//			return drawable;
//		}
//	};
//	private ImageGetter imgGetterVangua = new ImageGetter() {
//
//		public Drawable getDrawable(String source) {
//			Drawable drawable = null;
//			drawable = mContext.getResources().getDrawable(R.drawable.u21_vanguard_icon);
//			drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//			return drawable;
//		}
//	};

	static class ViewHolder {
		ImageCacheView imgAvatar;
		ImageCacheView imgPennants;
		ImageCacheView imgThumbnail;
		TextView txtMessage;
		TextView txtTimePost;
		RichTextView txtComment;
		Button btnReply;
		RelativeLayout layoutComment;
	}

	public String[] Temp(String string) {
		return string.split(",");
	}
}
